<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .box1,
        .box2 {
            position: relative;
            width: 400px;
            height: 200px;
            border: 1px solid #333;
            margin: 0 auto;
            margin-top: 20px;
        }
        
        .son1,
        .son2 {
            width: 100px;
            height: 200px;
            position: absolute;
            left: 50%;
            top: 0;
        }
        /* child1盒子通过position+margin来实现盒子水平居中  */
        
        .son1 {
            background-color: pink;
            margin-left: -50px;
        }
        /* child2盒子通过position+translate来实现盒子水平居中*/
        
        .son2 {
            background-color: royalblue;
            transform: translate(-50%);
        }
    </style>
</head>

<body>
    <div class="box1">
        <div class="son1"></div>
    </div>
    <div class="box2">
        <div class="son2"></div>
    </div>
</body>

</html>